﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;

namespace WAU.Controllers
{
    /// <summary>
    /// 访问需要授权文件的控制器
    /// </summary>    
    public class StaticController : Controller
    {
        /**
         * 如果需要指定别的路径的文件授权访问，则需要添加此控制器的类似方法
         */

        /// <summary>
        /// 取得 /Static/Images/IDCard 下的文件
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [Route("[controller]/Images/IDCard/{id?}")]
        [HttpGet]
        public ActionResult GetImagesIDCard(string id)
        {
            var file = Path.Combine(Directory.GetCurrentDirectory(), "static", "Images", "IDCard", id);
            FileInfo fi = new FileInfo(file);
            if (fi.Exists)
            {
                return PhysicalFile(file, "image/png"); // TODO 此处应当动态判断 content-type
            }
            else
            {
                return NotFound();
            }            
        }

        /// <summary>
        /// 取得 /Static/Images/IDCard 下的文件
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [Route("[controller]/Images/WXHeadImg/{id?}")]
        [HttpGet]
        public ActionResult GetImagesWXHeadImg(string id)
        {
            var file = Path.Combine(Directory.GetCurrentDirectory(), "static", "Images", "WXHeadImg", id);
            FileInfo fi = new FileInfo(file);
            if (fi.Exists)
            {
                return PhysicalFile(file, "image/png"); // TODO 此处应当动态判断 content-type
            }
            else
            {
                return NotFound();
            }
        }

        /// <summary>
        /// 取得 /Static/Upload/SGCAAG 下的文件
        /// </summary>
        /// <returns></returns>
        [Route("[controller]/Upload/SGCAAAG/{month?}/{id?}")]
        [HttpGet]
        public ActionResult GetSGCAAGFile(string month, string id)
        {
            var file = Path.Combine(Directory.GetCurrentDirectory(), "static", "Upload", "SGCAAAG", month, id);
            FileInfo fi = new FileInfo(file);
            if (fi.Exists)
            {
                return PhysicalFile(file, "application/octet-stream");
            }
            else
            {
                return NotFound();
            }
        }

        /// <summary>
        /// 取得用户头像文件
        /// </summary>
        /// <returns></returns>
        [Route("[controller]/Upload/HeadImages/{month?}/{id?}")]
        [HttpGet]
        public ActionResult GetHeadImages(string month, string id)
        {
            var file = Path.Combine(Directory.GetCurrentDirectory(), "static", "Upload", "HeadImages", month, id);
            FileInfo fi = new FileInfo(file);
            if (fi.Exists)
            {
                return PhysicalFile(file, "image/png"); // TODO 此处应当动态判断 content-type
            }
            else
            {
                return NotFound();
            }
        }

        /// <summary>
        /// 取得商品主图文件 
        /// </summary>
        /// <returns></returns>
        [Route("[controller]/Upload/CommodiyMainImage/{month?}/{id?}")]
        [HttpGet]
        public ActionResult GetCommodiyMainImages(string month, string id)
        {
            var file = Path.Combine(Directory.GetCurrentDirectory(), "static", "Upload", "CommodiyMainImage", month, id);
            FileInfo fi = new FileInfo(file);
            if (fi.Exists)
            {
                return PhysicalFile(file, "image/png"); // TODO 此处应当动态判断 content-type
            }
            else
            {
                return NotFound();
            }
        }

        /// <summary>
        /// 取得商品图片文件
        /// </summary>
        /// <returns></returns>
        [Route("[controller]/Upload/CommodiyImages/{month?}/{id?}")]
        [HttpGet]
        public ActionResult GetCommodiyImages(string month, string id)
        {
            var file = Path.Combine(Directory.GetCurrentDirectory(), "static", "Upload", "CommodiyImages", month, id);
            FileInfo fi = new FileInfo(file);
            if (fi.Exists)
            {
                return PhysicalFile(file, "image/png"); // TODO 此处应当动态判断 content-type
            }
            else
            {
                return NotFound();
            }
        }
    }
}
